package com.atguigu.spzx.order.controller;

import com.atguigu.spzx.model.dto.order.OrderDto;
import com.atguigu.spzx.model.entity.order.OrderInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.order.OrderTradeVo;
import com.atguigu.spzx.order.service.OrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author syh
 * @create 2023-09-03 16:18
 */
///api/order/orderInfo/auth/trade
@RestController
@Tag(name = "订单模块")
@RequestMapping("api/order/orderInfo")
public class OrderController {
    @Resource
    OrderService orderService;

    @Operation(summary = "根据订单编号更新订单支付状态")
    @GetMapping("/auth/updateOrderByOrderNo/{orderNo}")
    public Result updateOrderByOrderNo(@PathVariable("orderNo")Long orderNo){
        orderService.updateOrderByOrderNo(orderNo);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "提交订单编号查询订单")
    @GetMapping("/auth/findOrderByOrderNo/{orderNo}")
    public Result findOrderByOrderNo(@PathVariable("orderNo")Long orderNo){
        OrderInfo orderInfo = orderService.findOrderByOrderNo(orderNo);
        return Result.build(orderInfo,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "提交订单成功后查询订单")
    @GetMapping("/auth/{id}")
    public Result findOrderById(@PathVariable("id")Long id){
        OrderInfo orderInfo = orderService.findOrderById(id);
        return Result.build(orderInfo,ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "查询下单的订单详情")
    @GetMapping("/auth/trade")
    public Result trade(){
        OrderTradeVo orderTradeVo=orderService.trade();
        return Result.build(orderTradeVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "提交订单：用户确认下单 需要创建保存订单")
    @PostMapping("/auth/submitOrder")
    public Result submitOrder(@RequestBody OrderDto orderDto){
        Long orderId=orderService.submitOrder(orderDto);
        return Result.build(orderId,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "立即购买")
    @GetMapping("/auth/buy/{skuId}")
    public Result buy(@PathVariable("skuId")Long skuId){
        //用户在商品页面中点击立即购买 跳转到 订单确认页
        OrderTradeVo orderTradeVo = orderService.buy(skuId);
        return Result.build(orderTradeVo,ResultCodeEnum.SUCCESS);

    }

    @Operation(summary = "根据订单状态查询订单分页数据")
    @GetMapping("/auth/{page}/{limit}")
    public Result page(@PathVariable("page")Integer page,
                       @PathVariable("limit")Integer limit,
                       Integer orderStatus){
        PageInfo<OrderInfo> pageInfo= orderService.page(page,limit,orderStatus);
        return Result.build(pageInfo,ResultCodeEnum.SUCCESS);
    }
}
